# This file is part of pyDC software
# Copyright 2011
#
# pyDC is released under the terms of GPL licence.
# modified by Pavel C <pavelch+dcbot@gmail.com>
# src site: https://code.google.com/p/dc-bot/

#
from DCWorker import *
from DCSettings import ParseError, DCSettings
from DCHub import *

def connectTo(hub):
	if hub.__class__ == str:
		newHub = DCHub(hub)
		getWorker().addJob(newHub)
		return newHub

	elif hub.__class__ == list:
		if len(hub) == 0: return []

		w = getWorker()
		hubs = []

		for addr in hub:
			newHub = DCHub(addr)
			hubs.append(newHub)
			w.addJob(newHub)

		return hubs

	else:
		print("Parameter not supported.")
		return None

#import signal
#import sys

#def signal_handler(signal, frame):
#	getWorker().stop()
#	sys.exit(0)


if __name__ == "__main__":
	try:
		w = getWorker()
		if sys.argv[1]==None:exit
		w.loadSettings(sys.argv[1]+'/settings.xml')
	#except LocalIPError:
	#	print "Unable to retrieve local IP. Try using the 'ip' field in configuration file."
	#	sys.exit(1)
	except ParseError as e:
		print("Got an error while reading 'settings.xml': %s." % e.description)
		sys.exit(1)
	except:
		print("Unexpected error:", sys.exc_info())	
		sys.exit(1)
	if sys.argv[2]==None:exit
	w.logdir=sys.argv[2]
	#print w.getSettings().getHubs()
	x=w.getSettings().getHubs()
	#connectTo(x[1][0])	
	connectTo(x[0][0])
#	signal.signal(signal.SIGINT, signal_handler)
	w.run()
